package com.base.myproject.client.busi.print;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.UIObject;

/**
 * 打印输出到打印设备
 * <pre>
 * Generic printing class
 * can be used to print the Window it self, DOM.Elements, UIObjects (Widgets) and plain HTML
 * 
 * Usage:
 * You must insert this iframe in your host page:
 * 	&lt;iframe id=&quot;__printingFrame&quot; style=&quot;width:0;height:0;border:0&quot;&gt;&lt;/iframe&gt;
 * 
 * Window:
 * 	Print.it();
 * 
 * Objects/HTML:
 * 	Print.it(RootPanel.get(&quot;myId&quot;));
 * 	Print.it(DOM.getElementById(&quot;myId&quot;));
 * 	Print.it(&quot;Just &lt;b&gt;Print.it()&lt;/b&gt;!&quot;);
 * 
 * Objects/HTML using styles:
 * 	Print.it(&quot;&lt;link rel='StyleSheet' type='text/css' media='paper' href='/paperStyle.css'&gt;&quot;, RootPanel.get('myId'));
 * 	Print.it(&quot;&lt;style type='text/css' media='paper'&gt; .newPage { page-break-after: always; } &lt;/style&gt;&quot;,
 * 									&quot;Hi&lt;p class='newPage'&gt;&lt;/p&gt;By&quot;);
 * </pre>
 */
public class Print {

    /**
     * If true, use a Timer instead of DeferredCommand to print the internal fram
     */
    public static boolean USE_TIMER     = false;

    /**
     * Time in seconds to wait before printing the internal frame when using Timer
     */
    public static int TIMER_DELAY       = 2;


    public static native void it() /*-{
        $wnd.print();
    }-*/;

    public static void it(UIObject obj) {
        it("", obj);
    }

    public static void it(Element element) {
        it("", element);
    }

    public static void it(String style, UIObject obj) {
        it(style, obj.getElement());
    }

    public static void it(String style, Element element) {
        it("", style, element);
    }

    public static void it(String docType, String style, Element element) {
        it(docType, style, DOM.toString(element));
    }

    public static void it(String docType, String style, String it) {
        it(
                "<html>"
                +"<head>"
                +"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
                +"<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">"
                +"<link rel=\"stylesheet\" type=\"text/css\" href=\""+style+"\" />"
                +"</head>"+"<body>"
                +    it
                +"</body>"+
                "</html>");
    }

    public static void it(String html) {
        try {
            buildFrame(html);

//            if (USE_TIMER) {
//                Timer timer     = new Timer() {
//                        public void run() {
//                            printFrame();
//                        }
//                    };
//                timer.schedule(TIMER_DELAY * 1000);
//            } else {
//                DeferredCommand.addCommand(new Command() {
//                        public void execute() {
//                            printFrame();
//                        }
//                    });
//            }

        } catch (Throwable exc) {
            Window.alert(exc.getMessage());
        }
    }

    public static native void buildFrame(String html) /*-{
        var frame = $doc.getElementById('__printingFrame');
        if (!frame) {
            $wnd.alert("Error: Can't find printing frame.");
            return;
        }
        var doc = frame.contentWindow.document;
        doc.open();
        doc.write(html);
        doc.close();

    }-*/;

    public static native void printFrame() /*-{
        var frame = $doc.getElementById('__printingFrame');
        frame = frame.contentWindow;
        frame.focus();
        frame.print();
    }-*/;
}
